﻿using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.AsmType;
using System;

namespace AgencyManagerSystem.Core
{
    public class ProductionUpdaterCreator : BaseUpdaterCreator<ProductionModel>
    {
        public ProductionUpdater CreateProductionUpdaterInstance()
        {
            ProductionUpdater productionUpdater = new ProductionUpdater(new ProductionFetcher());
            productionUpdater.DataUpdating += FlagAndApproveFlagValid;
            productionUpdater.DataUpdating += DataFieldValid;
            productionUpdater.DataDeleting += OrderExists;
            productionUpdater.DataDeleting += CombinExists;
            productionUpdater.DataUpdating += CheckIfConflict;
            return productionUpdater;
        }

        private void CheckIfConflict(BaseUpdater<ProductionModel> sender, DataUpdatingEventArgs<ProductionModel> e)
        {
            if (e.Insert == null) return;
            if(e.Insert.Combination&&(e.Insert.Dimension1|| e.Insert.Dimension2 || e.Insert.Dimension3 || e.Insert.Dimension4 ||
                e.Insert.Dimension5 || e.Insert.Dimension6 || e.Insert.Dimension7 || e.Insert.Dimension8 ||
                e.Insert.Dimension9 || e.Insert.Dimension10 ))
            {
                e.Message = String.Format("产品档案{0}组合装与维度定义不能同时为True", e.Insert.AutoId);
                e.Cancel = true;
                return;
            }
        }

        private void CombinExists(BaseUpdater<ProductionModel> sender, DataDeletingEventArgs<ProductionModel> e)
        {
            if (e.Delete == null) return;
            var combinFetcher = new CombinationFetcher();
            var eleFetcher = new ElementFetcher();
            var combin = combinFetcher.GetByProduction(e.Delete.AutoId);
            if(combin!=null)
            {
                e.Message = String.Format("产品档案{0}已创建组合档案{1}，不能删除", e.Delete.AutoId, combin.AutoId);
                e.Cancel = true;
                return;
            }
            var ele = eleFetcher.ListByProduction(e.Delete.AutoId);
            if (ele.Count > 0)
            {
                e.Message = String.Format("产品档案{0}已被组合明细行{1}等{2}笔记录使用，不能删除", e.Delete.AutoId, ele[0].AutoId, ele.Count);
                e.Cancel = true;
                return;
            }
        }

        private void OrderExists(BaseUpdater<ProductionModel> sender, DataDeletingEventArgs<ProductionModel> e)
        {
            if (e.Delete == null)
                return;
            OrderDetailFetcher orderDetailFetcher = new OrderDetailFetcher();
            PuOrderDetailFetcher puOrderDetailFetcher = new PuOrderDetailFetcher();
            var orderDetails = orderDetailFetcher.ListByProduction(e.Delete.AutoId);
            if (orderDetails.Count>0)
            {
                e.Message = String.Format("产品档案{0}已被产品订单明细行{1}等{2}笔记录使用，不能删除",e.Delete.AutoId,orderDetails[0].AutoId,orderDetails.Count);
                e.Cancel = true;
                return;
            }
            var puorderDetails = puOrderDetailFetcher.ListByProduction(e.Delete.AutoId);
            if (puorderDetails.Count > 0)
            {
                e.Message = String.Format("产品档案{0}已被产品采购订单明细行{1}等{2}笔记录使用，不能删除", e.Delete.AutoId, puorderDetails[0].AutoId, puorderDetails.Count);
                e.Cancel = true;
                return;
            }
        }

        private void DataFieldValid(BaseUpdater<ProductionModel> sender, DataUpdatingEventArgs<ProductionModel> e)
        {
            if (e.Insert == null) return;
            if(String.IsNullOrWhiteSpace(e.Insert.Code)
                || String.IsNullOrWhiteSpace(e.Insert.Name)
                || String.IsNullOrWhiteSpace(e.Insert.Unit)
                || String.IsNullOrWhiteSpace(e.Insert.Specification))
            {
                e.Cancel = true;
                e.Message = "编号，产品名称，单位，规格不能为空";
                return;
            }
        }
    }
}